#include "IdleThread.h"
#include <iostream>
#include "ThreadSafePrinter.h"

HTHREADPOOL_NAMESPACE_BEGIN

    IdleThread::IdleThread(AtomicQueue<Task> * poolTaskQueue, AtomicPriorityQueue<Task> * poolPriorityTaskQueue)
    : ThreadPoolObject(ThreadType::kIdle, poolTaskQueue)
    {
    }

    IdleThread::~IdleThread()
    {
        ThreadSafePrinter threadSafePrinter;
        threadSafePrinter.print("%s", __FUNCTION__);
        exit();
    }

    void IdleThread::run()
    {
        while(true) {
            {
                std::unique_lock<std::mutex> lock(m_mutex);
                m_cv.wait(lock, [this] {
                    return m_state.load() == ThreadState::kRunning || m_state.load() == ThreadState::kTerminated;
                });
                if(m_state.load() == ThreadState::kTerminated) {
                    break;
                }
            }

            ThreadSafePrinter threadSafePrinter;
            threadSafePrinter.print("idle thread running.......%d", static_cast<int>(m_type));
            std::this_thread::sleep_for(std::chrono::milliseconds(1));
        }
    }

    void IdleThread::excuteTask() {}
    void IdleThread::excuteTasks() {}
    void IdleThread::exit(){quit();}

HTHREADPOOL_NAMESPACE_END
